package com.linkage.jz.bankruptcy.fragment.Project;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Project.EditManagerUserInfoActivity;
import com.linkage.jz.bankruptcy.activity.Project.EditProjectDetailActivity;
import com.linkage.jz.bankruptcy.activity.Project.ProDetailActivity;
import com.linkage.jz.bankruptcy.adapter.MyProListViewAdapter;
import com.linkage.jz.bankruptcy.adapter.RelationProListViewAdapter;
import com.linkage.jz.bankruptcy.bean.ManagerBaseUserInfo;
import com.linkage.jz.bankruptcy.bean.ManagerUserInfo;
import com.linkage.jz.bankruptcy.bean.MyProInfo;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.jz.bankruptcy.widget.PublicNoticeDialog;
import com.linkage.jz.bankruptcy.widget.RecyclerViewNoBugLinearLayoutManager;
import com.linkage.seapeak.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGAOnItemChildClickListener;
import cn.bingoogolapple.androidcommon.adapter.BGAOnRVItemClickListener;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author FT
 * @time 2016-07-18 16:49
 */

// JUMP 
@SuppressLint("ValidFragment")
public class RelationProjectFragment extends BaseFragment implements BGAOnRVItemClickListener{
    RecyclerView mRecyclerView;
    View empty_layout;

    private RelationProListViewAdapter mAdapter;
    private ManagerUserInfo mInfo;

    private List<MyProInfo> proInfoList;

    public static RelationProjectFragment newInstance(ManagerUserInfo info) {
        RelationProjectFragment tripFragment = new RelationProjectFragment(info);
        return tripFragment;
    }

    public RelationProjectFragment(ManagerUserInfo info) {
        mInfo = info;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        proInfoList = new ArrayList<>();
        initAdapter(proInfoList);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_relation_project, container, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.mRecyclerView);
        empty_layout = view.findViewById(R.id.empty_layout);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    /**
     * 设置RecyclerView属性
     */
    private void initAdapter(List<MyProInfo> data) {
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new RelationProListViewAdapter(mRecyclerView, getActivity());
        mAdapter.setData(proInfoList);
        mAdapter.setNeedAction(false);
        mAdapter.setOnRVItemClickListener(this);
        RecyclerViewNoBugLinearLayoutManager layoutManager = new RecyclerViewNoBugLinearLayoutManager(getActivity());
        layoutManager.setOrientation(RecyclerViewNoBugLinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);//设置adapter
    }


    @Override
    public void onRVItemClick(ViewGroup parent, View itemView, int position) {
        try {
            if (position < mAdapter.getData().size()) {
                MyProInfo model = mAdapter.getItem(position);
                if (model != null) {

                    Intent intent = new Intent(getActivity(), ProDetailActivity.class);
                    intent.putExtra("info", new Gson().toJson(model));
                    getActivity().startActivity(intent);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void getData() {
        Network.getAPIService()
                .relationProject(getUserToken(), mInfo.getSysUserId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        try {
                            if(json.get("code").getAsInt() == 0) {
                                JsonArray data = json.getAsJsonArray("data");
                                List<MyProInfo> list = MyProInfo.parse(data);
                                proInfoList.clear();
                                proInfoList.addAll(list);
                                mAdapter.notifyDataSetChanged();
                                empty_layout.setVisibility(proInfoList.size() == 0 ? View.VISIBLE : View.GONE);
                            } else {
                                onError(null);
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        proInfoList.clear();
                        mAdapter.notifyDataSetChanged();
                        empty_layout.setVisibility(View.VISIBLE);
                    }
                });
    }
}
